/*
 * Copyright (c) 2021 柯华栋
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IPC_UTIL_H
#define IPC_UTIL_H

#include <gio/gio.h>
#include <stdlib.h>
#include "ipc_data.h"

void ipc_init(void);

void ipc_lock(void);

void ipc_unlock(void);

// (返回值由map拥有，不要手动释放)
const gchar* module_get_id_by_map(ipc_map_t* map);

// (返回值需要手动释放)
const gchar* module_get_name_by_id(const gchar* id);

// (返回值需要手动释放)
const gchar* module_get_name_by_map(ipc_map_t* map);

// 用于on_post, on_send, handle_update中快速寻找id关联的处理函数 ---------------------------
typedef struct _id_pointer_map_t id_pointer_map_t;
typedef struct _id_pointer_map_t id_s64_map_t;

id_pointer_map_t* 	id_pointer_map_new(void);
void 				id_pointer_map_free(	id_pointer_map_t* map);
gpointer 			id_pointer_map_get(		id_pointer_map_t* map, const gchar* id);
void				id_pointer_map_set(		id_pointer_map_t* map, const gchar* id, gpointer pointer);
void 				id_pointer_map_remove(	id_pointer_map_t* map, const gchar* id);

int	id_pointer_map_size(id_pointer_map_t* map);
const gpointer id_pointer_map_get_pointer_by_pos(id_pointer_map_t* map, int pos);
void id_pointer_map_set_pointer_by_pos(id_pointer_map_t* map, int pos, gpointer pointer);

#define id_s64_map_new 		id_pointer_map_new
#define id_s64_map_free		id_pointer_map_free
#define id_s64_map_remove	id_pointer_map_remove

#define id_s64_map_get(map, id)	\
	((gint64)id_pointer_map_get(map, id))
	
#define id_s64_map_set(map, id, number) \
	id_pointer_map_set(map, id, (gpointer)number)
	

#endif // IPC_UTIL_H
